package com.moon.icoresso.strategy;

import com.moon.icoresso.pojo.bo.OauthTokenHandleBO;
import com.moon.icoresso.pojo.dto.OauthTokenDTO;
import com.moon.icoresso.pojo.dto.OauthTokenParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName OauthTokenStrategyContext
 * @Description
 * @Author menbbo
 * @Date 2021/2/13 17:59
 * @Version V1.0
 **/
@Component
public class OauthTokenStrategyContext {
    private Map<String,OauthStrategy> map = new ConcurrentHashMap<>();

    /**
     * 将OauthStrategy类型的对象注入到map中
     * @param map
     */
    @Autowired
    public OauthTokenStrategyContext(Map<String,OauthStrategy> map){
        this.map.clear();
        map.forEach(this.map::put);
    }

    public void checkParam(String beanName, OauthTokenParam oauthTokenParam, OauthTokenHandleBO oauthTokenHandleBO){
        if(StringUtils.isNotBlank(beanName)){
            map.get(beanName).checkParam(oauthTokenParam,oauthTokenHandleBO);
        }
    }

    public OauthTokenDTO generateToken(String beanName,OauthTokenParam oauthTokenParam, OauthTokenHandleBO oauthTokenHandleBO){
        if(StringUtils.isNotBlank(beanName)){
            return map.get(beanName).handle(oauthTokenParam,oauthTokenHandleBO);
        }
        return null;
    }
}